package cn.tedu.boot.ers.controller;

import cn.tedu.boot.ers.exception.ServiceException;
import cn.tedu.boot.ers.pojo.dto.AdminAddDTO;
import cn.tedu.boot.ers.pojo.dto.AdminLoginDTO;
import cn.tedu.boot.ers.pojo.dto.AdminUpdateNameDTO;
import cn.tedu.boot.ers.pojo.dto.AdminUpdatePasswordDTO;
import cn.tedu.boot.ers.pojo.vo.AdminSimpleVO;
import cn.tedu.boot.ers.service.IAdminService;
import cn.tedu.boot.ers.web.JsonResult;
import cn.tedu.boot.ers.web.State;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping(value = "/admins", produces = "application/json;charset=utf-8")
@Api(tags = "管理员模块")
public class AdminController {
    @Autowired
    IAdminService service;

    @ApiOperation("添加管理员")
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(AdminAddDTO adminAddDTO) {
        service.addNew(adminAddDTO);
        return JsonResult.ok();
    }

    @ApiOperation("管理员登录")
    @PostMapping("/login")
    public JsonResult<Void> login(AdminLoginDTO adminLoginDTO, HttpSession session) {
        log.info("adminLoginDTO = " + adminLoginDTO);
        service.login(adminLoginDTO, session);
        return JsonResult.ok();
    }

    @ApiOperation("管理员修改名字")
    @PostMapping("/update-name")
    public JsonResult<Void> updateNameById(AdminUpdateNameDTO adminUpdateNameDTO) {
        log.info("adminUpdateNameDTO = " + adminUpdateNameDTO);
        service.updateNameById(adminUpdateNameDTO);
        return JsonResult.ok();
    }

    @ApiOperation("管理员修改密码")
    @PostMapping("/update-password")
    public JsonResult<Void> updatePassword(AdminUpdatePasswordDTO adminUpdatePasswordDTO) {
        log.info("adminUpdateNameDTO = " + adminUpdatePasswordDTO);
        service.updatePassword(adminUpdatePasswordDTO);
        return JsonResult.ok();
    }

    @ApiOperation("客户端获取当前会话中的对象")
    @GetMapping("/current-admin")
    public JsonResult<AdminSimpleVO> currentUser(HttpSession session) {
        //把登录成功时保存的用户对象取出 返回给客户端
        Object admin = session.getAttribute("admin");
        if (admin == null){
            throw new ServiceException(State.ERR,"当前没有登录对象");
        }
        System.out.println("admin = " + admin);
        return JsonResult.ok((AdminSimpleVO) session.getAttribute("admin"));
    }

    @ApiOperation("客户端清空当前会话中的对象/登出")
    @PostMapping("/logout")
    public JsonResult<Void> logout(HttpSession session) {
        Object admin = session.getAttribute("admin");
        if (admin == null){
            throw new ServiceException(State.ERR,"当前没有登录对象");
        }
        log.warn("即将退出当前admin");
        //把登录成功时保存的用户对象删除
        session.removeAttribute("admin");
        return JsonResult.ok();
    }
}

